#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import shutil
import subprocess
from pathlib import Path

home = Path().home()

# 定义要运行的命令及参数
command = ['command', '-v', 'nvim']


def sh_com(command):
    try:
        # 创建子进程并执行命令
        process = subprocess.Popen(command, stdout=subprocess.PIPE)

        # 等待子进程结束并获取标准输出
        output, error = process.communicate()

        if process.returncode == 0:
            # print("命令成功执行")

            # 打印命令输出内容
            return output.decode('utf-8')
        else:
            print("命令执行失败")
    except Exception as e:
        print("发生错误:", str(e))


def copy_files(src_dir, dest_dir):
    # 创建目标目录
    os.makedirs(dest_dir, exist_ok=True)
    # 使用os.walk遍历源目录中的所有文件和子目录
    for root, dirs, files in os.walk(src_dir):
        for file_name in files:
            src_file = os.path.join(root, file_name)
            dest_file = os.path.join(dest_dir, file_name)
            # 复制文件
            shutil.copy2(src_file, dest_file)


output = sh_com(command).strip()
realpath = Path(output).resolve()
doc_path = str(Path(realpath).parent.parent) + "/share/nvim/runtime/doc"
# print(realpath, doc_path)
# need to install vimcdoc plugin first,to download the chinese help doc(help-translated)
source_dir = f"{home}/.local/share/nvim/lazy/vimcdoc/doc"

copy_files(source_dir, doc_path)
